<template>
  <div class="app-container">
    <!-- 搜索条件 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="108px">
      <el-form-item label="时段类型" prop="timeSlot">
        <el-select v-model="queryParams.timeSlot" placeholder="请选择" clearable>
          <el-option
            v-for="item in timeSlotOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 顶部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="时段类型" align="center" prop="timeSlot" :show-overflow-tooltip="true">
        <template slot-scope="scope">{{ selectDictLabel(timeSlotOptions, scope.row.timeSlot) }}</template>
      </el-table-column>
      <el-table-column
        label="考勤开始时间"
        align="center"
        prop="attendanceStartTime"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="考勤结束时间"
        align="center"
        prop="attendanceEndTime"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="modifyTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifyTime) }}</span>
        </template>
      </el-table-column>

      <!-- 表格操作列 -->
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            circle
            type="success"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          />
          <el-button
            size="mini"
            circle
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页条 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增 OR 修改对话框 -->
    <el-dialog :title="title" v-if="open" :visible.sync="open" width="35%">
      <el-form ref="form" :model="form" label-width="120px" :rules="rules">
        <el-form-item label="考勤时间类型" prop="timeSlot">
          <el-select v-model="form.timeSlot" placeholder="请选择" style="width: 80%">
            <el-option
              v-for="item in timeSlotOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="attendanceStartTime" :key="1">
          <el-time-select
            v-model="form.attendanceStartTime"
            placeholder="开始时间"
            value-format="HH:mm"
            style="width: 80%"
            :picker-options="{
              start: '00:00',
              step: '00:05',
              end: '23:59',
            }"
          ></el-time-select>
        </el-form-item>
        <el-form-item label="结束时间" prop="attendanceEndTime" :key="2">
          <el-time-select
            v-model="form.attendanceEndTime"
            placeholder="结束时间"
            value-format="HH:mm"
            style="width: 80%"
            :picker-options="{
              start: '00:00',
              step: '00:05',
              end: '23:59',
              minTime: form.attendanceStartTime,
            }"
          ></el-time-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  findPageList,
  addRules,
  updateRules,
  deleteRules,
  findAttendanceRulesById
} from "@/api/attendance/rules";

export default {
  name: "rules",
  data() {
    return {
      dialogVisible: true,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 数据列表
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        timeSlot: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        timeSlot: [{ required: true, message: "必填", trigger: "blur" }],
        attendanceStartTime: [
          { required: true, message: "必填", trigger: "blur" }
        ],
        attendanceEndTime: [
          { required: true, message: "必填", trigger: "blur" }
        ]
      },
      //考勤时间段类型
      timeSlotOptions: undefined
    };
  },

  created() {
    this.getDicts("time_slot").then(response => {
      this.timeSlotOptions = response.data;
    });
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      findPageList(this.queryParams).then(response => {
        this.dataList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 新增
    handleAdd() {
      this.title = "新增考勤规则";
      this.open = true;
      this.reset();
    },
    // 提交表单重置
    reset() {
      this.form = {
        id: undefined,
        timeSlot: undefined,
        attendanceStartTime: undefined,
        attendanceEndTime: undefined
      };
      this.resetForm("form");
    },
    // 修改
    handleUpdate(row) {
      this.title = "修改考勤规则";
      this.open = true;
      this.reset();
      findAttendanceRulesById(row.id).then(resp => {
        this.form = resp.data;
        console.log(this.form);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 新增 OR 修改 提交
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        console.log(this.form);
        if (this.form.id) {
          if (valid) {
            updateRules(this.form).then(resp => {
              if (resp.code === 200) {
                this.msgSuccess(resp.message);
                this.open = false;
                this.getList();
              } else {
                this.msgError(resp.message);
              }
            });
          }
        } else {
          if (valid) {
            addRules(this.form).then(resp => {
              if (resp.code === 200) {
                this.msgSuccess(resp.message);
                this.open = false;
                this.getList();
              } else {
                this.msgError(resp.message);
              }
            });
          }
        }
      });
    },
    // 删除
    handleDelete(row) {
      deleteRules(row.id).then(resp => {
        this.msgSuccess(resp.message);
        this.open = false;
        this.getList();
      });
    }
  }
};
</script>

<style>
</style>